package org.darcy.service.base.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.darcy.entity.base.BaseDicType;
import org.darcy.framework.generic.GenericServiceImpl;
import org.darcy.framework.model.PageData;
import org.darcy.framework.model.PageParam;
import org.darcy.framework.model.Query;
import org.darcy.service.base.BaseDicTypeService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Service
public class BaseDicTypeServiceImpl extends GenericServiceImpl<BaseDicType> implements BaseDicTypeService {

	@Override
	public List<BaseDicType> getAll(BaseDicType dicType) {
		LambdaQueryWrapper<BaseDicType> dicTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
		if (StringUtils.isNotBlank(dicType.getName())) {
			dicTypeLambdaQueryWrapper.like(BaseDicType::getName, dicType.getName()).or().like(BaseDicType::getCode,
					dicType.getCode());
		}
		dicTypeLambdaQueryWrapper.orderByDesc(BaseDicType::getOrderNo);
		return this.getList(dicTypeLambdaQueryWrapper);
	}

	@Override
	public PageData<BaseDicType> page(PageParam<BaseDicType> param) {
		BaseDicType dicType = param.getEntity();
		Query query = param.getQuery();
		LambdaQueryWrapper<BaseDicType> dicTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
		if (StringUtils.isNotBlank(dicType.getName())) {
			dicTypeLambdaQueryWrapper.like(BaseDicType::getCode, dicType.getName()).or().like(BaseDicType::getName,
					dicType.getName());
		}
		dicTypeLambdaQueryWrapper.orderByAsc(BaseDicType::getOrderNo);
		IPage<BaseDicType> queryPage = new Page<>(query.getPageNum(), query.getPageSize());
		IPage<BaseDicType> page = this.getPage(queryPage, dicTypeLambdaQueryWrapper);
		PageData<BaseDicType> pageData = new PageData<BaseDicType>(page, param);
		return pageData;
	}

}
